package com.ease.plugin.pluginClasses {
  import com.ease.core.plugin.IPluggable;
  import com.ease.core.plugin.PluginInfo;
  import com.ease.plugin.PluginFactory;
  import com.ease.plugin.traits.LoadState;
  import com.ease.plugin.traits.LoadTrait;
  import com.ease.plugin.traits.LoaderBase;

  import flash.display.Loader;

  import mx.core.Application;

  internal class PluginLoader extends LoaderBase {
    public function PluginLoader(pluginFactory:PluginFactory) {
      this.pluginFactory = pluginFactory;
    }

    protected function unloadFromPluginInfo(pluginInfo:PluginInfo):void {
    }

    protected function loadFromPluginInfo(loadTrait:LoadTrait,
                                            app:Application, loader:Loader = null):void {
         loadTrait.resource.target = loader;

         var pluginInfo:* = null;
         if (app is IPluggable) {
            pluginInfo = IPluggable(app).pluginInfo;
         }
      if (pluginInfo != null) {
        loadTrait.resource.pluginInfo = pluginInfo;

        pluginInfo.initializePlugin(loadTrait.resource);
      }
      var pluginLoadTrait:PluginLoadTrait = loadTrait as PluginLoadTrait;
      pluginLoadTrait.pluginInfo = pluginInfo;
      pluginLoadTrait.loader = loader;
      updateLoadTrait(pluginLoadTrait, LoadState.READY);

    }

    protected function isPluginCompatible(pluginInfo:Object):Boolean {
      return true;
    }

    private function isPluginVersionSupported(pluginVersion:String):Boolean {
      return 	true;
    }

    private var pluginFactory:PluginFactory;
  }
}